package com.netease.qa.biz.service.impl;

import com.nestease.qa.dao.api.ProjectDAO;
import com.nestease.qa.dao.api.TeamDAO;
import com.nestease.qa.dao.api.TestCaseDAO;
import com.nestease.qa.dao.api.UserTeamDAO;
import com.nestease.qa.dao.entity.Project;
import com.nestease.qa.dao.entity.Team;
import com.nestease.qa.dao.entity.TestCase;
import com.nestease.qa.dao.params.ProjectQueryParams;
import com.nestease.qa.dao.params.TestCaseQueryParams;
import com.netease.qa.biz.dto.ProjectDTO;
import com.netease.qa.biz.enums.ProjectStatus;
import com.netease.qa.biz.enums.TestCaseStatus;
import com.netease.qa.biz.service.ProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/2/28
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectDAO projectDAO;
    @Resource
    private TeamDAO teamDAO;
    @Resource
    private UserTeamDAO userTeamDAO;
    @Resource
    private TestCaseDAO testCaseDAO;

    public void addProject(ProjectDTO projectDTO) {
        Project project = new Project();
        project.setProjectId(projectDTO.getProjectId());
        project.setProjectName(projectDTO.getProjectName());
        project.setTeamId(projectDTO.getTeamId());
        project.setEnv(projectDTO.getEnv());
        project.setIp(projectDTO.getIp());
        project.setPort(projectDTO.getPort());
        project.setCreator(projectDTO.getCreator());
        project.setLastEditor(projectDTO.getCreator());
        project.setStatus(ProjectStatus.Valid.getValue());
        projectDAO.addProject(project);
    }

    public int getNextProjectId() {
        int maxProjectId = projectDAO.getMaxProjectId();
        return ++maxProjectId;
    }

    public List<ProjectDTO> getAllProject() {
        List<ProjectDTO> result = new ArrayList<ProjectDTO>();
        ProjectDTO projectDTO;

        List<Project> projectList = projectDAO.getAllProject();
        for (Project project : projectList
                ) {
            projectDTO = new ProjectDTO();
            projectDTO.setProjectId(project.getProjectId());
            projectDTO.setProjectName(project.getProjectName());
            projectDTO.setEnv(project.getEnv());
            projectDTO.setIp(project.getIp());
            projectDTO.setPort(project.getPort());
            //获取团队信息
            Team team = teamDAO.getTeamDetail(project.getTeamId());
            projectDTO.setTeamName(team.getTeamName());
            result.add(projectDTO);
        }
        return result;
    }

    public ProjectDTO getProjectDetail(int projectId) {
        Project project = projectDAO.getProjectDetail(projectId);
        if (project != null) {
            ProjectDTO projectDTO = new ProjectDTO();
            projectDTO.setProjectId(project.getProjectId());
            projectDTO.setProjectName(project.getProjectName());
            projectDTO.setEnv(project.getEnv());
            projectDTO.setIp(project.getIp());
            projectDTO.setPort(project.getPort());
            //获取团队信息
            Team team = teamDAO.getTeamDetail(project.getTeamId());
            projectDTO.setTeamName(team.getTeamName());
            projectDTO.setTeamId(team.getTeamId());
            return projectDTO;
        } else {
            return null;
        }
    }

    public void updateProject(ProjectDTO projectDTO) {
        Project project = new Project();
        project.setProjectId(projectDTO.getProjectId());
        project.setProjectName(projectDTO.getProjectName());
        project.setTeamId(projectDTO.getTeamId());
        project.setEnv(projectDTO.getEnv());
        project.setIp(projectDTO.getIp());
        project.setPort(projectDTO.getPort());
        project.setLastEditor(projectDTO.getLastEditor());
        projectDAO.updateProject(project);
    }

    public String validateBeforeDelete(int projectId) {
        //项目下是否有有效的测试用例
        TestCaseQueryParams testCaseQueryParams = new TestCaseQueryParams();
        testCaseQueryParams.setProjectId(projectId);
        testCaseQueryParams.setStatus(TestCaseStatus.Valid.getValue());
        List<TestCase> testCaseDTOList = testCaseDAO.getTestCaseInCondition(testCaseQueryParams);

        if (testCaseDTOList != null && testCaseDTOList.size() > 0) {
            return "该项目下还有有效的测试用例,删除后会将所有数据置为无效,确认删除吗?";
        }
        return "验证通过";
    }

    @Transactional
    public void deleteProject(int projectId, String userId) {
        //项目下是否有有效的测试用例
        TestCaseQueryParams testCaseQueryParams = new TestCaseQueryParams();
        testCaseQueryParams.setProjectId(projectId);
        testCaseQueryParams.setStatus(TestCaseStatus.Valid.getValue());
        List<TestCase> testCaseDTOList = testCaseDAO.getTestCaseInCondition(testCaseQueryParams);
        if (testCaseDTOList != null) {
            //删除测试用例
            for (TestCase testCase : testCaseDTOList
                    ) {
                testCase.setLastEditor(userId);
                testCaseDAO.deleteTestCase(testCase);
            }
        }

        //删除项目
        Project project = projectDAO.getProjectDetail(projectId);
        project.setLastEditor(userId);
        projectDAO.deleteProject(project);
    }

    public List<ProjectDTO> getUserRelatedProjectList(String userId) {
        List<ProjectDTO> result = new ArrayList<ProjectDTO>();
        ProjectDTO projectDTO;

        //找到这个用户参与了哪些组
        List<Integer> joinedTeamIdList = userTeamDAO.getJoinedTeamIdList(userId);
        for (Integer joinedTeamId : joinedTeamIdList
                ) {
            //找到这些组下分别有哪些项目
            ProjectQueryParams params = new ProjectQueryParams();
            params.setTeamId(joinedTeamId);
            params.setStatus(ProjectStatus.Valid.getValue());
            List<Project> projectList = projectDAO.getProjectListInCondition(params);
            for (Project project : projectList
                    ) {
                projectDTO = new ProjectDTO();
                projectDTO.setProjectId(project.getProjectId());
                projectDTO.setProjectName(project.getProjectName());
                projectDTO.setEnv(project.getEnv());
                result.add(projectDTO);
            }
        }
        return result;
    }

    public boolean confirmEditProjectPermission(int projectId, String userId) {
        Project project = projectDAO.getProjectDetail(projectId);
        List<String> joinedUserIdList = userTeamDAO.getJoinedUserIdList(project.getTeamId());
        if (joinedUserIdList.contains(userId)) {
            return true;
        }
        return false;
    }

    public List<ProjectDTO> getProjectListInCondition(ProjectQueryParams params) {
        List<Project> projectList = projectDAO.getProjectListInCondition(params);
        if (projectList != null) {
            List<ProjectDTO> result = new ArrayList<ProjectDTO>();
            ProjectDTO projectDTO;

            for (Project project : projectList
                    ) {
                projectDTO = new ProjectDTO();
                projectDTO.setProjectId(project.getProjectId());
                projectDTO.setProjectName(project.getProjectName());
                projectDTO.setEnv(project.getEnv());
                projectDTO.setIp(project.getIp());
                projectDTO.setPort(project.getPort());
                result.add(projectDTO);
            }
            return result;
        }
        return null;
    }
}
